const db = require('../mysql/index')

exports.getCustomerList = (req, res, next) => {
    const { name } = req.body
    if (name) {
        let sql2 = 'select * from customer where name like ?'
        db.query(sql2, name, (err, result) => {
            if (result) {
                res.send({
                    msg: '查询成功',
                    code: 200,
                    data: result
                })
            } else {
                res.send({
                    msg: err,
                    code: 400
                })
            }
        })
    } else {
        let sql1 = 'select * from customer'
        db.query(sql1, (err, result) => {
            if (result) {
                res.send({
                    msg: '查询成功',
                    code: 200,
                    data: result
                })
            } else {
                res.send({
                    msg: err,
                    code: 400
                })
            }
        })
    }

}

exports.addOrEditCustomer = (req, res, next) => { 
    const { id, name, sex, phone } = req.body
    if (id) {
        let sql1 = 'update customer set name = ?,sex = ?,phone = ? where id = ?'
        db.query(sql1, [name, sex, phone, id], (err, result) => {
            if (result) {
                res.send({
                    msg: '修改成功',
                    code: 200
                })
            } else {
                res.send({
                    msg:err,
                })
            }
        })
    }else {
        let sql2 = 'insert into customer (name,sex,phone) values (?,?,?)'
        db.query(sql2, [name, sex, phone], (err, result) => {
            if (result) {
                res.send({
                    msg: '添加成功',
                    code: 200
                })
            } else {
                res.send({
                    msg:err,
                })
            }
        })
    }
}